package service

import (
	"ksd-social-api/modules/note/model"
	"ksd-social-api/modules/note/model/context"
	"ksd-social-api/modules/note/model/vo"
	"ksd-social-api/utils"
)

type NoteStudyUserRecordsService struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存用户课程学习记录
 */
func (service *NoteStudyUserRecordsService) SaveNoteStudyUserRecordsChapter(ctx *context.NoteStudyUserRecordsContext) bool {
	// 判断用户是否学习过该节
	noteRecordsCount := noteUserRecordsMapper.CountNoteStudyUserRecordsChapter(ctx.SpecialId, ctx.UserId, ctx.NoteId, ctx.ChapterId, ctx.SystemId)
	// 如果==0代表没有学习过，就保存
	if noteRecordsCount == 0 {
		var model model.NoteStudyUserRecords
		utils.CopyProperties(&model, ctx)
		// 保存用户下的学习节的记录
		return noteUserRecordsMapper.SaveNoteStudyUserRecords(&model)
	}
	return false
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存用户课程学习记录
 */
func (service *NoteStudyUserRecordsService) SaveNoteStudyUserRecordsLesson(ctx *context.NoteStudyUserRecordsContext) bool {
	// 判断用户是否学习过该节
	noteRecordsCount := noteUserRecordsMapper.CountNoteStudyUserRecordsLesson(ctx.SpecialId, ctx.UserId, ctx.NoteId, ctx.ChapterId, ctx.LessonId, ctx.SystemId)
	// 如果==0代表没有学习过，就保存
	if noteRecordsCount == 0 {
		var model model.NoteStudyUserRecords
		utils.CopyProperties(&model, ctx)
		// 保存用户下的学习节的记录
		return noteUserRecordsMapper.SaveNoteStudyUserRecords(&model)
	}
	return false
}

/**
 * 查询课程和用户学习课程记录
 * @author feige
 * @date 2023-12-17
 * @version 1.0
 * @desc
 */
func (service *NoteStudyUserRecordsService) FindNoteStudyUserRecordsByUN(userId uint64, noteId uint64, systemId uint64) []*vo.NoteStudyUserRecordsVo {
	records := noteUserRecordsMapper.FindNoteStudyUserRecordsByUN(userId, noteId, systemId)
	var vos []*vo.NoteStudyUserRecordsVo
	for _, record := range records {
		var noteUserRecordsVo vo.NoteStudyUserRecordsVo
		utils.CopyProperties(&noteUserRecordsVo, record)
		vos = append(vos, &noteUserRecordsVo)
	}
	return vos
}

/**
 * 查询用户学习课程记录
 * @author feige
 * @date 2023-12-17
 * @version 1.0
 * @desc
 */
func (service *NoteStudyUserRecordsService) FindNoteStudyUserRecords(userId uint64, specialId uint64, systemId uint64) []*vo.NoteStudyUserRecordsVo {
	records := noteUserRecordsMapper.FindNoteStudyUserRecords(userId, specialId, systemId)
	var vos []*vo.NoteStudyUserRecordsVo
	for _, record := range records {
		var noteUserRecordsVo vo.NoteStudyUserRecordsVo
		utils.CopyProperties(&noteUserRecordsVo, record)
		vos = append(vos, &noteUserRecordsVo)
	}
	return vos
}
